package com.example.controller;

import cn.hutool.core.util.ObjectUtil;
import com.example.common.Result;
import com.example.model.dto.AddTeacherRequest;
import com.example.model.dto.LoginRequest;
import com.example.model.dto.RegisterRequest;
import com.example.model.entity.UserInfo;
import com.example.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/backend/user")
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping("/current")
    public Result<UserInfo> getCurrent(HttpSession session){
        UserInfo user = (UserInfo) session.getAttribute("user");
        if(ObjectUtil.isNull(user)){
            return Result.error("用户未登录");
        }
        return Result.success(user);
    }

    @PostMapping("/login")
    public Result<UserInfo> login(@RequestBody LoginRequest loginRequest, HttpSession session) {
        UserInfo userInfo = userService.login(loginRequest);
        session.setAttribute("user", userInfo);
        return Result.success(userInfo);
    }

    @PostMapping("/register")
    public Result<UserInfo> register(@RequestBody RegisterRequest registerRequest) {
        return Result.success(userService.register(registerRequest));
    }

    @PostMapping("/teacher")
    public Result<UserInfo> addTeacher(@RequestBody AddTeacherRequest addTeacherRequest) {
        return Result.success(userService.addTeacher(addTeacherRequest));
    }

    /**
     * 管理员获取所有老师的信息
     * @param page
     * @param limit
     * @return
     */
    @GetMapping("/teacher/list")
    public Result<List<UserInfo>> teacherList(Integer page,Integer limit) {
        return Result.success(userService.teacherList());
    }

    @PostMapping("/teacher/delete")
    public Result<Boolean> deleteTec(@RequestBody UserInfo userInfo) {
        return Result.success(userService.deleteTec(userInfo));
    }


    /**
     * 老师获取所有学生的信息
     * @return
     */
    @GetMapping("/studentList")
    public Result<List<UserInfo>> studentList() {
        return Result.success(userService.studentList());
    }
} 